{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "localtext = open('9.html', 'r', encoding='utf8')\n",
    "sp = BeautifulSoup(localtext, 'lxml')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<!DOCTYPE html>\n",
      "<html lang=\"en\">\n",
      "<head>\n",
      "<meta charset=\"utf-8\"/>\n",
      "<meta content=\"width=device-width, initial-scale=1.0\" name=\"viewport\"/>\n",
      "<meta content=\"ie=edge\" http-equiv=\"X-UA-Compatible\"/>\n",
      "<title>contents和children</title>\n",
      "</head>\n",
      "<body>\n",
      "<div id=\"divid\">\n",
      "<p>p1</p>\n",
      "<p>p2</p>\n",
      "<p>p3</p>\n",
      "<p>\n",
      "<span>s1</span>\n",
      "<span>s2</span>\n",
      "</p>\n",
      "<a href=\"a1\">a1</a>\n",
      "<a href=\"a2\">a2</a>\n",
      "<a href=\"a3\">a3</a>\n",
      "</div>\n",
      "</body>\n",
      "</html>\n"
     ]
    }
   ],
   "source": [
    "print(sp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "div = sp.find('div', {'id':'divid'})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<div id=\"divid\">\n",
      "<p>p1</p>\n",
      "<p>p2</p>\n",
      "<p>p3</p>\n",
      "<p>\n",
      "<span>s1</span>\n",
      "<span>s2</span>\n",
      "</p>\n",
      "<a href=\"a1\">a1</a>\n",
      "<a href=\"a2\">a2</a>\n",
      "<a href=\"a3\">a3</a>\n",
      "</div>\n"
     ]
    }
   ],
   "source": [
    "print(div)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['\\n', <p>p1</p>, '\\n', <p>p2</p>, '\\n', <p>p3</p>, '\\n', <p>\n",
      "<span>s1</span>\n",
      "<span>s2</span>\n",
      "</p>, '\\n', <a href=\"a1\">a1</a>, '\\n', <a href=\"a2\">a2</a>, '\\n', <a href=\"a3\">a3</a>, '\\n']\n"
     ]
    }
   ],
   "source": [
    "## 获取当前标签里面的子标签\n",
    "## div标签内的字符串，也是作为内容的一部分\n",
    "print(div.contents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<list_iterator object at 0x00000000087D1BC8>\n"
     ]
    }
   ],
   "source": [
    "## 列表的生成器\n",
    "print(div.children)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['\\n', <p>p1</p>, '\\n', <p>p2</p>, '\\n', <p>p3</p>, '\\n', <p>\n",
      "<span>s1</span>\n",
      "<span>s2</span>\n",
      "</p>, '\\n', <a href=\"a1\">a1</a>, '\\n', <a href=\"a2\">a2</a>, '\\n', <a href=\"a3\">a3</a>, '\\n']\n"
     ]
    }
   ],
   "source": [
    "print(list(div.children))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<p>p1</p>\n"
     ]
    }
   ],
   "source": [
    "print(div.contents[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['p1']\n"
     ]
    }
   ],
   "source": [
    "print(div.contents[1].contents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "div.contents[0]\n",
    "## '\\n' 没有contents属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<p>\n",
       "<span>s1</span>\n",
       "<span>s2</span>\n",
       "</p>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "div.contents[7]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['\\n', <span>s1</span>, '\\n', <span>s2</span>, '\\n']"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "div.contents[7].contents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<span>s1</span>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "div.contents[7].contents[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['s1']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "div.contents[7].contents[1].contents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['\\n', <span>s1</span>, '\\n', <span>s2</span>, '\\n']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "div.contents[7].contents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'list' object has no attribute 'contents'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-17-1bceca67c5b1>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m## 列表没有contents属性\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mdiv\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcontents\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m7\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcontents\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcontents\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: 'list' object has no attribute 'contents'"
     ]
    }
   ],
   "source": [
    "## 列表没有contents属性\n",
    "div.contents[7].contents.contents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
